<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function factorial(n) {
            if (n === 0 || n === 1) {
                return 1
            }
            return n * factorial(n - 1)

        }

        var cache = {}
        function factorialCatche(n) {
            if (cache[n]) {
                return cache[n]
            }
            if (n === 0 || n === 1) {
                return (cache[0] = cache[1] = 1)
            }
            return (cache[n] = n * factorial(n - 1))
        }
        function memorize(fn) {
            var cache = {}
            return function () {
                var key = fn.name + '_' + [].join.call(arguments)
                return cache[key] = cache[key] || fn.apply(this,arguments)
            }

        }

        var factorialmemorize=memorize(factorial)

        console.time("factorial")
        factorial(5000)
        console.timeEnd("factorial")


        console.time("factorialCatche")
        factorialmemorize(5000)
        console.timeEnd("factorialCatche")

        console.time("factorialCatche-second")
        factorialmemorize(5000)
        console.timeEnd("factorialCatche-second")
    </script>
</body>

</html>